package com.ror.java.se.inet.udp;

//聊天室练习(接收端)
//  UDP发送数据：数据来自键盘录入，直到输入的数据是886，发送数据结束
//  UDP接收数据：因为接收端不知道发送端什么时候停止发送，故采用死循环接收

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Demo02UdpChatReceiver {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(10086);
        byte[] msgBuf = new byte[1024];
        DatagramPacket dp = new DatagramPacket(msgBuf, msgBuf.length);
        String msg = null;
        while (!"886".equals(msg)) {
            ds.receive(dp);
            msg = new String(dp.getData(), 0, dp.getLength());
            System.out.println("[" + dp.getAddress() + ":" + dp.getPort() + "]" + msg);
        }
        ds.close();
        System.out.println("(会话已结束)");
    }
}
